package com.tensquare.search.service;

import com.tensquare.search.dao.ArticleDao;
import com.tensquare.search.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.Date;

@Service
public class ArticleService {
    @Autowired
    private ArticleDao articleDao;
    @Autowired
    private IdWorker idWorker;
    public void save(Article article){
        article.setId(idWorker.nextId()+"");
        article.setCreatetime(new Date());
        article.setThumbup(0);
        article.setVisit(0);
        article.setComment(0);
        articleDao.save(article);
    }
    //通过关键字查询文章（带分页）
    public Page<Article> findByKey(String key,int page,int size){
        Pageable pageable= PageRequest.of(page-1,size);
        return  articleDao.findByTitleOrContentLike(key,key,pageable);
    }

}
